<!DOCTYPE html>
<html lang="en">
    <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="format-detection" content="telephone=no" />

  <title>
    04|基础篇--经常说的CPU上下文切换是什么意思 | 迪克猪的博客
  </title>

  
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
  <link rel="manifest" href="/manifest.json" />
  <meta name="theme-color" content="#ffffff" />

  
  <link
    rel="stylesheet"
    href="https://unpkg.com/modern-normalize@0.6.0/modern-normalize.css"
  />

  
  
  
  
  <link rel="stylesheet" href="https://zsy619.github.io/style.min.f7761d111b74dd5c07f0111decee92938c12abc42e0fd319e1a07483e248b54e.css" integrity="sha256-93YdERt03VwH8BEd7O6Sk4wSq8QuD9MZ4aB0g&#43;JItU4=" />

  
  
    
  
</head>

    <body>
        <header id="header">
  <div class="header_container">
    <h1 class="sitetitle">
      <a href="https://zsy619.github.io" title="迪克猪的博客">迪克猪的博客</a>
    </h1>
    <nav class="navbar">
      <ul>
        <li><a href="https://zsy619.github.io">Home</a></li>
        
          <li>
            <a href="/post/">
              
              <span>LINUX性能优化</span>
            </a>
          </li>
        
          <li>
            <a href="/csapp/">
              
              <span>深入理解计算机系统_第三版</span>
            </a>
          </li>
        
          <li>
            <a href="/golang/">
              
              <span>golang</span>
            </a>
          </li>
        
          <li>
            <a href="/docker/">
              
              <span>容器</span>
            </a>
          </li>
        
          <li>
            <a href="/flutter/">
              
              <span>Flutter</span>
            </a>
          </li>
        
          <li>
            <a href="/know/">
              
              <span>知识点</span>
            </a>
          </li>
        
          <li>
            <a href="/categories/">
              
              <span>目录</span>
            </a>
          </li>
        
          <li>
            <a href="/about/">
              
              <span>关于</span>
            </a>
          </li>
        
        <li class="hide-sm"><a href="https://zsy619.github.io/index.xml" type="application/rss+xml">RSS</a></li>
      </ul>
    </nav>
  </div>
</header>

        
<section id="main">
  <article class="post content">
    <h2 class="title">04|基础篇--经常说的CPU上下文切换是什么意思</h2>
    <div class="post_content">
      <h3 id="查看系统上下文切换情况">查看系统上下文切换情况</h3>
<p>上下文切换，会把cpu时间消耗在寄存器、内核栈及虚拟内存等数据的保存和恢复上，缩短进程真正的运行时间，成为系统性能大幅下降的一个元凶。</p>
<p>使用vmstat查看上下文切换情况，本工具主要分析系统的内存使用情况。例子每间隔5秒输出一组数据：
<img src="../../images/20200529-1115-33.png" alt=""></p>
<ul>
<li>cs（context switch）：每秒上下文切换的次数</li>
<li>in（interrupt）：每秒中断的次数</li>
<li>r（Running or Runnable）：就绪队列长度，也就是正在运行和等待cpu运行的进程</li>
<li>b（blocked）：处于不可中断睡眠状态的进程
vmstat给出系统整体的上下文切换情况，pidstat可以查看每个进程的详细情况</li>
</ul>
<p>间隔5秒输出一组数据：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ pidstat -w <span style="color:#ae81ff">5</span>
</code></pre></div><p><img src="../../images/20200529-1125-49.png" alt=""></p>
<ul>
<li>cswch：每秒自愿上下文切换（voluntary context switches）次数</li>
<li>nvcswch：每秒非自愿上下文切换（non voluntary context switches）次数</li>
</ul>
<h3 id="使用sysbench模拟多线程调度切换情况">使用sysbench模拟多线程调度切换情况</h3>
<p>安装sysbench：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ sudo apt install sysbench
</code></pre></div><p>第一个终端模拟多线程调度：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># 以10个线程运行5分钟的基准测试</span>
$ sysbench --threads<span style="color:#f92672">=</span><span style="color:#ae81ff">10</span> --max-time<span style="color:#f92672">=</span><span style="color:#ae81ff">300</span> threads run
</code></pre></div><p><img src="../../images/20200529-1149-32.png" alt=""></p>
<p>第二个终端监控上下文切换情况：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># 每间隔1秒输出一组数据</span>
$ vmstat <span style="color:#ae81ff">1</span>
</code></pre></div><p><img src="../../images/20200529-1151-50.png" alt=""></p>
<p>pidstat默认显示进程的指标数，加上-t显示线程的指标数</p>
<p>如何知道中断发生的类型？/proc/interrupts</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ watch -d cat /proc/interrupts
</code></pre></div><p><img src="../../images/20200529-1223-58.png" alt=""></p>
<ul>
<li>RES：重新调度中断，表示唤醒空闲状态cpu调度新的任务运行</li>
<li>IPI：处理器间中断，调度器分散任务到不同cpu的机制</li>
</ul>

    </div>
    <div class="info post_meta">
      <time datetime=2020-05-27T12:25:08&#43;0800 class="date">Wednesday, May 27, 2020</time>
      
        <ul class="tags">
        
          <li> <a href="https://zsy619.github.io/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96">性能优化</a> </li>
        
          <li> <a href="https://zsy619.github.io/tags/linux">linux</a> </li>
        
        </ul>
      
      
    </div>
    <div class="clearfix"></div>
  </article>
  
    <div class="other_posts">
      
      <a href="https://zsy619.github.io/post/03%E5%9F%BA%E7%A1%80%E7%AF%87%E7%BB%8F%E5%B8%B8%E8%AF%B4%E7%9A%84cpu%E4%B8%8A%E4%B8%8B%E6%96%87%E5%88%87%E6%8D%A2%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/" class="prev">03|基础篇经常说的CPU上下文切换是什么意思</a>
      
      
      <a href="https://zsy619.github.io/post/05%E5%9F%BA%E7%A1%80%E7%AF%87%E6%9F%90%E4%B8%AA%E5%BA%94%E7%94%A8%E7%9A%84cpu%E4%BD%BF%E7%94%A8%E7%8E%87%E5%B1%85%E7%84%B6%E8%BE%BE%E5%88%B0100%E6%88%91%E8%AF%A5%E6%80%8E%E4%B9%88%E5%8A%9E/" class="next">05|基础篇--某个应用的CPU使用率居然达到100%，我该怎么办？</a>
      
    </div>
    <aside id="comments">
</aside>

  
</section>

        <a id="back_to_top" title="Go To Top" href="#">
  <span>
    <svg viewBox="0 0 24 24">
      <path fill="none" d="M0 0h24v24H0z"></path>
      <path d="M12 2L4.5 20.29l.71.71L12 18l6.79 3 .71-.71z"></path>
    </svg>
  </span>
</a>

        <footer id="footer">
  <p>
    <span>&copy; 2021 <a href="https://zsy619.github.io" title="迪克猪的博客">迪克猪的博客</a> </span>
    <span>Built with <a rel="nofollow" target="_blank" href="https://gohugo.io">Hugo</a></span>
    <span>Theme by <a rel="nofollow" target="_blank" href="https://github.com/wayjam/hugo-theme-mixedpaper">WayJam</a></span>
  </p>

  <script src="https://zsy619.github.io/js/main.min.8b182175f5874aeed0acc0979345c98d4bde22208ec4f36cc1d6e3102acb4b10.js" integrity="sha256-ixghdfWHSu7QrMCXk0XJjUveIiCOxPNswdbjECrLSxA=" crossorigin="anonymous" async></script>
</footer>

    </body>
</html>
